<template>
  <div class="record">
    <el-form :inline="true" :model="recordform" class="record_form">
      <el-form-item>
        <el-select
          v-model="recordform.departmentId"
          clearable
          placeholder="所有科室"
          @change="getdoctorlist"
        >
          <el-option
            v-for="item in departmentlist"
            :key="item.id"
            :label="item.departmentName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="recordform.doctorId"
          clearable
          placeholder="所有医生"
        >
          <el-option
            v-for="item in doctorlist"
            :key="item.id"
            :label="item.doctorName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="recordform.diabetesType"
          clearable
          placeholder="全部类型"
        >
          <el-option
            v-for="item in diabetelist"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="recordform.patientName"
          placeholder="请填写姓名"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="recordform.hospitalNum"
          placeholder="请填写住院号"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="recordform.bedNum"
          placeholder="请填写床号"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="recordform.phone"
          placeholder="请填写手机号"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="recordform.measuringTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getRecordData" type="primary">搜索</el-button>
      </el-form-item>
    </el-form>
    <my-table :header="header" :tableList="tableData"></my-table>
    <v-pagination :total="total" @currentPage="currentPage"></v-pagination>
  </div>
</template>

<script>
import myTable from "@/components/Table";
// 引入分页组件
import vPagination from "../../components/pagination/index";

export default {
  data() {
    return {
      recordform: {
        departmentId: "", //科室
        doctorId: "", //医生
        diabetesType: "", // 类型
        patientName: "", //姓名
        hospitalNum: "", //住院号
        bedNum: "", //床号
        phone: "", //电话号
        startTime: "", //开始日期
        endTime: "", //结束日期
        measuringTime: "", //检测时间
      },
      // 科室下拉
      departmentlist: [],
      // 医生下拉
      doctorlist: [],
      // 所有类型下拉
      diabetelist: [
        {
          label: "无",
          value: "0",
        },
        {
          label: "1型",
          value: "1",
        },
        {
          label: "2型",
          value: "2",
        },
        {
          label: "妊娠",
          value: "3",
        },
        {
          label: "其他",
          value: "4",
        },
      ],
      // 检测下拉
      options_measuring: [
        {
          label: "2021-4-14",
          val: "1",
        },
      ],
      header: [
        { prop: "bedNum", label: "床号" },
        {
          prop: "patientName",
          label: "姓名",
        },
        {
          prop: "phone",
          label: "手机号",
          width: 120,
        },
        {
          prop: "departmentName",
          label: "科室",
        },
        {
          prop: "doctorName",
          label: "医生",
        },
        {
          prop: "hospitalNum",
          label: "住院号",
        },
        {
          prop: "sugarNum",
          label: "最近血糖（mmil/L）",
        },
        {
          prop: "inHospitalTime",
          label: "最新检测时间",
        },
        {
          prop: "programGlAttr",
          label: "检测段",
        },
        {
          label: "操作",
          render: (h, data) => {
            return (
              <el-button
                type="text"
                onClick={() => {
                  this.checkbloodSugar(data.row.id);
                }}
              >
                查看数据
              </el-button>
            );
          },
        },
      ],
      tableData: [],
      total: 0,
      page: 1,
    };
  },
  mounted() {
    this.getDepartment();
    this.getAllDataInfo(1, {});
  },
  methods: {
    // 模糊查询
    getRecordData() {
      if (
        this.recordform.measuringTime &&
        this.recordform.measuringTime.length > 0
      ) {
        this.recordform.startTime = this.recordform.measuringTime[0];
        this.recordform.endTime = this.recordform.measuringTime[1];
      } else {
        this.recordform.startTime = "";
        this.recordform.endTime = "";
      }
      this.getAllDataInfo(this.page, this.recordform);
    },
    // handleClick(row) {
    //   this.$router.push({ path: "/patientRecords", query: { id: "1" } });
    // },
    // 获取科室信息
    getDepartment() {
      let routeid = sessionStorage.getItem("routeID");
      let perId = routeid ? JSON.parse(routeid)["/manageData"] : "";
      let data = {
        perId,
      };
      this.$api.getDepartment(data).then((res) => {
        this.departmentlist = res.data.data;
      });
    },
    // 获取医生列表
    getdoctorlist(val) {
      if (!val) {
        this.doctorlist = [];
        this.recordform.doctor = "";
        return;
      }
      let doctorlist = this.departmentlist.filter((item) => {
        return item.id == val;
      });
      if (doctorlist[0].bloodSugarDoctorList[0].doctorName) {
        this.doctorlist = doctorlist[0].bloodSugarDoctorList;
      } else {
        this.doctorlist = [];
      }
    },
    // 获取数据管理列表
    getAllDataInfo(page, recordform) {
      let data = {};
      data = recordform;
      let current = page ? page : 1;
      data.current = current;
      this.$api
        .getAllDataInfo(data)
        .then((res) => {
          this.tableData = res.data.data;
          this.total = res.total;
        })
        .catch((err) => {
          this.tableData = [];
          this.total = 0;
        });
    },
    // 当前页码
    currentPage(page) {
      this.page = page;
      this.getRecordData(this.page, this.recordform);
    },
    // 查看指定患者详情
    checkbloodSugar(id) {
      this.$router.push({ path: "/patientRecords", query: { id } });
    },
  },
  components: {
    myTable,
    vPagination,
  },
};
</script>

<style lang="scss" scoped>
.record {
  height: 100%;
  background: #fff;
  padding: 25px 31px;
  .record_form {
    .el-form-item {
      & >>> .el-form-item__content {
        .el-input {
          width: 197px;
        }
      }
    }
  }
}
</style>